package com.twsjw.dao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.twsjw.entity.District;
import com.twsjw.entity.Houseinfo;
import com.twsjw.entity.Street;
import com.twsjw.entity.Userinfo;
import com.twsjw.util.HibernateSessionFactory;

public class houseDAOImpl implements houseDAO{

	public List select(String userid) {
		List list=new ArrayList();
		Session session=HibernateSessionFactory.getSession();
		String hql="from Houseinfo where userinfo.userId='"+userid+"'";
		list=session.createQuery(hql).list();
		session.close();
		return list;
	}
	public List HQL(String hql) {
		List list=new ArrayList();
		Session session=HibernateSessionFactory.getSession();
		list=session.createQuery(hql).list();
		session.close();
		return list;
	}

	public int delinfo(String houseid) {
		Session session=HibernateSessionFactory.getSession();
		String hql="delete from Houseinfo where houseId='"+houseid+"'";
		Transaction tran=session.beginTransaction();
		int i=session.createQuery(hql).executeUpdate();
		tran.commit();
		session.close();
		return i;
	}

	public int insert(Houseinfo h, String district_id, String street_id,String userid) {
		int i=-1;
		
		Session session=HibernateSessionFactory.getSession();
		District d=(District)session.get(District.class,new Integer(district_id));
		Street s=(Street)session.get(Street.class,new Integer(street_id));
		Userinfo u=(Userinfo)session.get(Userinfo.class,new Integer(userid));
		
		h.setDistrict(d);
		h.setStreet(s);
		h.setUserinfo(u);
		
		Transaction tran=session.beginTransaction();
		
		session.save(h);
		
		tran.commit();
		
		session.close();
		return i;
	}

	public Houseinfo selhouse(String houseid) 
	{
		Houseinfo h=null;
		String hql="from Houseinfo where houseId='"+houseid+"'";
		Session session=HibernateSessionFactory.getSession();
		List list=session.createQuery(hql).list();
		if(list.size()>0){h=(Houseinfo)list.get(0);}
		session.close();
		return h;
	}
	
	public int updateinfo(Houseinfo hinfo,String districtid,String streetid,String houseid,Timestamp ts){
		int i=1;
		
		Session session=HibernateSessionFactory.getSession();
		/*district.districtId='"+districtid+"'," + "street.streetId = '"
      			+ streetid + "',
*/		String sql = "update Houseinfo set room = '" + hinfo.getRoom()+ "',hall = '" + hinfo.getHall() + "', floor = '"
		      			+ hinfo.getFloor() + "', area = '" + hinfo.getArea()+ "', house_age= '" + hinfo.getHouseAge()
		      			+ "',price = '"+ hinfo.getPrice() + "',title='"+hinfo.getTitle()+"',date='"+ts+"',telephone = '"
		      			+ hinfo.getTelephone() + "', contract_man = '"
		      			+ hinfo.getContractMan() + "',remark='"
		      			+ hinfo.getRemark() + "' where house_id = '"
		      			+ houseid + "'";
		Transaction tran=session.beginTransaction();
		i=session.createQuery(sql).executeUpdate();
		tran.commit();
		session.close();
		
		return i;
		
	}
}
